package com.ziq.zcamp.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.BarUtils
import com.scwang.smart.refresh.horizontal.HorizontalFooter
import com.ziq.base.extension.click
import com.ziq.base.extension.dp
import com.ziq.zcamp.R
import com.ziq.zcamp.bean.post.PostingDetail
import com.ziq.zcamp.databinding.LayoutPlaceDetailPostBannerBinding
import com.ziq.zcamp.manager.GlobalContextManager
import com.ziq.zcamp.manager.PostManager
import com.ziq.zcamp.place.PlaceDetailPunchRecommendAdapter
import com.ziq.zcamp.place.PlacePunchActivity

class PlaceDetailPostBannerView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr){

    var binding : LayoutPlaceDetailPostBannerBinding? = null
    var campId :Int? = null
    var mPlaceDetailPunchRecommendAdapter : PlaceDetailPunchRecommendAdapter? = null
    var start = false
    init {
        binding = LayoutPlaceDetailPostBannerBinding.inflate(LayoutInflater.from(context), this, true)
        GlobalContextManager.getCurrentActivity()?.let {
            binding?.container?.setPadding(0, BarUtils.getStatusBarHeight(), 0, 0)
        }
        binding?.container?.click {
            onDismiss()
        }
        binding?.smartRefreshHorizontal?.setEnableRefresh(false)
        binding?.smartRefreshHorizontal?.setEnableLoadMore(false)
        binding?.smartRefreshHorizontal?.setEnableAutoLoadMore(false)
        binding?.smartRefreshHorizontal?.setFooterTriggerRate(0.6f)
        binding?.smartRefreshHorizontal?.setRefreshFooter(
            HorizontalFooter(inflate(context, R.layout.footer_home_subject, null)),
            30.dp, 30.dp)
        binding?.smartRefreshHorizontal?.setOnLoadMoreListener {
            binding?.smartRefreshHorizontal?.finishLoadMore(0)
            binding?.smartRefreshHorizontal?.resetNoMoreData()
            GlobalContextManager.getCurrentActivity()?.let {
                PlacePunchActivity.open(it,campId?:0)
            }
        }

        binding?.rvPost?.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
        mPlaceDetailPunchRecommendAdapter = PlaceDetailPunchRecommendAdapter(context)
        binding?.rvPost?.adapter = mPlaceDetailPunchRecommendAdapter
        mPlaceDetailPunchRecommendAdapter?.adapterAction =
            object : PlaceDetailPunchRecommendAdapter.AdapterAction {
                override fun itemClick(item: PostingDetail) {
                    GlobalContextManager.getCurrentActivity()?.let {
                        PostManager.navigateToDetailActivity(it, item)
                    }
                }

                override fun moreClick() {
                    GlobalContextManager.getCurrentActivity()?.let {
                        PlacePunchActivity.open(it,campId?:0)
                    }
                }
            }

        binding?.bannerReactionView?.updateBehavior(onSetting = {
            it?.onNestedPreScroll = { coordinatorLayout: CoordinatorLayout,
                                      child: View,
                                      target: View,
                                      dx: Int,
                                      dy: Int,
                                      consumed: IntArray,
                                      type: Int ->
                if(binding?.rvPost?.canScrollHorizontally(-1) == false){
                    if(!start && dx < 0){
                        start = true
                        setXOffset(dx.toFloat())
                        consumed[0] = dx
                    } else if(start){
                        setXOffset(dx.toFloat())
                        consumed[0] = dx
                    }
                }

            }
            it?.onStopNestedScroll = {coordinatorLayout: CoordinatorLayout,
                                      child: View,
                                      target: View,
                                      type: Int ->
                if(start){
                    start = false
                    innerStopXOffset()
                }
            }
        })
    }



    fun updateData(campId: Int, list: List<PostingDetail>){
        this.campId = campId
        binding?.smartRefreshHorizontal?.setEnableLoadMore(list.size > 5)
        mPlaceDetailPunchRecommendAdapter?.setData(list)
    }



    fun setXOffset(dx : Float){
        var temp = (binding?.container?.translationX?:0f) - dx
        if(temp < 0f){
            temp = 0f
        }
        binding?.container?.translationX = temp
        if(binding?.container?.visibility == View.GONE){
            binding?.container?.visibility = View.VISIBLE
        }
    }

    fun resetXOffset(){
        post {
            binding?.container?.translationX = (binding?.root?.width?:0).toFloat()
            binding?.container?.visibility = View.GONE
        }
    }

    fun stopXOffset(){
        if((binding?.container?.translationX?:0f) > ((binding?.root?.width?:0).toFloat() * 0.75f)){
            onDismiss()
        } else {
            onShow()
        }
    }

    private fun innerStopXOffset(){
        if((binding?.container?.translationX?:0f) > ((binding?.root?.width?:0).toFloat() * 0.25f)){
            onDismiss()
        } else {
            onShow()
        }
    }

    var onShowActionListener : (()->Unit)? = null
    var onDismissActionListener : (()->Unit)? = null

    fun onShow(){
        binding?.container?.translationX = 0f
        onShowActionListener?.invoke()
    }

    fun onDismiss(){
        resetXOffset()
        onDismissActionListener?.invoke()
    }

}